\documentclass{article}\usepackage[]{graphicx}\usepackage[]{color}
%% maxwidth is the original width if it is less than linewidth
%% otherwise use linewidth (to make sure the graphics do not exceed the margin)
\makeatletter
\def\maxwidth{ %
  \ifdim\Gin@nat@width>\linewidth
    \linewidth
  \else
    \Gin@nat@width
  \fi
}
\makeatother

\definecolor{fgcolor}{rgb}{0.345, 0.345, 0.345}
\newcommand{\hlnum}[1]{\textcolor[rgb]{0.686,0.059,0.569}{#1}}%
\newcommand{\hlstr}[1]{\textcolor[rgb]{0.192,0.494,0.8}{#1}}%
\newcommand{\hlcom}[1]{\textcolor[rgb]{0.678,0.584,0.686}{\textit{#1}}}%
\newcommand{\hlopt}[1]{\textcolor[rgb]{0,0,0}{#1}}%
\newcommand{\hlstd}[1]{\textcolor[rgb]{0.345,0.345,0.345}{#1}}%
\newcommand{\hlkwa}[1]{\textcolor[rgb]{0.161,0.373,0.58}{\textbf{#1}}}%
\newcommand{\hlkwb}[1]{\textcolor[rgb]{0.69,0.353,0.396}{#1}}%
\newcommand{\hlkwc}[1]{\textcolor[rgb]{0.333,0.667,0.333}{#1}}%
\newcommand{\hlkwd}[1]{\textcolor[rgb]{0.737,0.353,0.396}{\textbf{#1}}}%

\usepackage{framed}
\makeatletter
\newenvironment{kframe}{%
 \def\at@end@of@kframe{}%
 \ifinner\ifhmode%
  \def\at@end@of@kframe{\end{minipage}}%
  \begin{minipage}{\columnwidth}%
 \fi\fi%
 \def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep
 \colorbox{shadecolor}{##1}\hskip-\fboxsep
     % There is no \\@totalrightmargin, so:
     \hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
 \MakeFramed {\advance\hsize-\width
   \@totalleftmargin\z@ \linewidth\hsize
   \@setminipage}}%
 {\par\unskip\endMakeFramed%
 \at@end@of@kframe}
\makeatother

\definecolor{shadecolor}{rgb}{.97, .97, .97}
\definecolor{messagecolor}{rgb}{0, 0, 0}
\definecolor{warningcolor}{rgb}{1, 0, 1}
\definecolor{errorcolor}{rgb}{1, 0, 0}
\newenvironment{knitrout}{}{} % an empty environment to be redefined in TeX

\usepackage{alltt}

\input{../labStyle}
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}

\begin{document}

\license{This is a product of OpenIntro that is released under a Creative Commons Attribution-ShareAlike 3.0 Unported (\web{http://creativecommons.org/licenses/by-sa/3.0/}). This lab was written for OpenIntro by Andrew Bray and Mine \c{C}etinkaya-Rundel.}

\section*{Lab 4B: Foundations for statistical inference - Confidence levels}

\subsection*{Sampling from Ames, Iowa}
If you have access to data on an entire population, say the size of every house in Ames, Iowa, it's straight forward to answer questions like, ``How big is the typical house in Ames?'' and ``How much variation is there in sizes of houses?''. If you have access to only a sample of the population, as is often the case, the task becomes more complicated. What is your best guess for the typical size if you only know the sizes of several dozen houses? This sort of situation requires that you use your sample to make inference on what your population looks like.

\subsection*{The data}

In the previous lab we looked at the population data of houses from Ames, Iowa. Let's start by loading that data set.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{download.file}\hlstd{(}\hlstr{"http://www.openintro.org/stat/data/ames.RData"}\hlstd{,} \hlkwc{destfile} \hlstd{=} \hlstr{"ames.RData"}\hlstd{)}

\hlkwd{load}\hlstd{(}\hlstr{"ames.RData"}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}


In this lab we'll start with a simple random sample of size 60 from the population. Specifically, this is a simple random sample of size 60. Note that the data set has information on many housing variables, but for the first portion of the lab we'll focus on the size of the house, represented by the variable \hlkwd{Gr.Liv.Area}.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{population} \hlkwb{<-} \hlstd{ames}\hlopt{$}\hlstd{Gr.Liv.Area}

\hlstd{samp} \hlkwb{<-} \hlkwd{sample}\hlstd{(population,} \hlnum{60}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}


\begin{exercise}
Describe the distribution of your sample. What would you say is the ``typical" size within your sample? Also state precisely what you interpreted ``typical" to mean.
\end{exercise}

\begin{exercise}
Would you expect another student's distribution to be identical to yours? Would you expect it to be similar? Why, or why not?
\end{exercise}

\subsection*{Confidence intervals}
One of the most common ways to describe the typical or central value of a distribution is to use the mean. In this case we can calculate the mean of the sample using,

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{sample_mean} \hlkwb{<-} \hlkwd{mean}\hlstd{(samp)}
\end{alltt}
\end{kframe}
\end{knitrout}


Return for a moment to the question that first motivated this lab: based on this sample, what can we infer about the population? Based only on this single sample, the best estimate of the average living area of houses sold in Ames would be the sample mean, usually denoted as $\bar{x}$ (here we're calling it \hlkwd{sample\_mean}). That serves as a good \emph{point estimate} but it would be useful to also communicate how uncertain we are of that estimate. This can be captured by using a \emph{confidence interval}.

We can calculate a 95\% confidence interval for a sample mean by adding and subtracting 1.96 standard errors to the point estimate.\symbolfootnote[2]{See Section 4.2.3 if you are unfamiliar with this formula.}

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{se} \hlkwb{<-} \hlkwd{sd}\hlstd{(samp)}\hlopt{/}\hlkwd{sqrt}\hlstd{(}\hlnum{60}\hlstd{)}

\hlstd{lower} \hlkwb{<-} \hlstd{sample_mean} \hlopt{-} \hlnum{1.96} \hlopt{*} \hlstd{se}

\hlstd{upper} \hlkwb{<-} \hlstd{sample_mean} \hlopt{+} \hlnum{1.96} \hlopt{*} \hlstd{se}

\hlkwd{c}\hlstd{(lower, upper)}
\end{alltt}
\end{kframe}
\end{knitrout}


This is an important inference that we've just made: even though we don't know what the full population looks like, we're 95\% confident that the true average size of houses in Ames lies between the values \hlkwd{lower} and \hlkwd{upper}. There are a few conditions that must be met for this interval to be valid.

\begin{exercise}
For the confidence interval to be valid, the sample mean must be normally distributed and have standard error $s / \sqrt{n}$. What conditions must be met for this to be true?
\end{exercise}

\subsection*{Confidence levels}

\begin{exercise}
What does ``95\% confidence" mean? If you're not sure, see Section 4.2.2.
\end{exercise}

In this case we have the luxury of knowing the true population mean since we have data on the entire population. This value can be calculated using the following command:

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{mean}\hlstd{(population)}
\end{alltt}
\end{kframe}
\end{knitrout}


\begin{exercise}
Does your confidence interval capture the true average size of houses in Ames? If you are working on this lab in a classroom, does your neighbor's interval capture this value? 
\end{exercise}

\begin{exercise}
Each student in your class should have gotten a slightly different confidence interval. What proportion of those intervals would you expect to capture the true population mean? Why? If you are working in this lab in a classroom, collect data on the intervals created by other students in the class and calculate the proportion of intervals that capture the true population mean.
\end{exercise}

Using R, we're going to recreate many samples to learn more about how sample means and confidence intervals vary from one sample to another. \emph{Loops} come in handy here.\symbolfootnote[4]{If you are unfamiliar with loops, review \href{http://www.openintro.org/stat/labs/04A_Intro_to_Statistical_Inference.pdf}{Lab 4A}.}

Here is the rough outline:
\begin{enumerate}[(1)]
\item Obtain a random sample.
\item Calculate the sample's mean and standard deviation.
\item Use these statistics to calculate a confidence interval.
\item Repeat steps (1)-(3) 50 times.
\end{enumerate}

But before we do all of this, we need to first create empty vectors where we can save the means and standard deviations that will be calculated from each sample. And while we're at it, let's also store the desired sample size as \hlkwd{n}.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{samp_mean} \hlkwb{<-} \hlkwd{rep}\hlstd{(}\hlnum{NA}\hlstd{,} \hlnum{50}\hlstd{)}

\hlstd{samp_sd} \hlkwb{<-} \hlkwd{rep}\hlstd{(}\hlnum{NA}\hlstd{,} \hlnum{50}\hlstd{)}

\hlstd{n} \hlkwb{<-} \hlnum{60}
\end{alltt}
\end{kframe}
\end{knitrout}


Now we're ready for the loop where we calculate the means and standard deviations of 50 random samples.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwa{for}\hlstd{(i} \hlkwa{in} \hlnum{1}\hlopt{:}\hlnum{50}\hlstd{)\{}
  \hlstd{samp} \hlkwb{<-} \hlkwd{sample}\hlstd{(population, n)} \hlcom{# obtain a sample of size n = 60 from the population}
  \hlstd{samp_mean[i]} \hlkwb{<-} \hlkwd{mean}\hlstd{(samp)}    \hlcom{# save sample mean in ith element of samp_mean}
  \hlstd{samp_sd[i]} \hlkwb{<-} \hlkwd{sd}\hlstd{(samp)}        \hlcom{# save sample sd in ith element of samp_sd}
\hlstd{\}}
\end{alltt}
\end{kframe}
\end{knitrout}


Lastly, we construct the confidence intervals.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{lower_vector} \hlkwb{<-} \hlstd{samp_mean} \hlopt{-} \hlnum{1.96} \hlopt{*} \hlstd{samp_sd}\hlopt{/}\hlkwd{sqrt}\hlstd{(n)}

\hlstd{upper_vector} \hlkwb{<-} \hlstd{samp_mean} \hlopt{+} \hlnum{1.96} \hlopt{*} \hlstd{samp_sd}\hlopt{/}\hlkwd{sqrt}\hlstd{(n)}
\end{alltt}
\end{kframe}
\end{knitrout}


Lower bounds of these 50 confidence intervals are stored in \hlkwd{lower\_vector}, and the upper bounds are in \hlkwd{upper\_vector}. Let's view the first interval.

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{c}\hlstd{(lower_vector[}\hlnum{1}\hlstd{], upper_vector[}\hlnum{1}\hlstd{])}
\end{alltt}
\end{kframe}
\end{knitrout}


\vspace{1.5cm}

\subsection*{On your own}

\begin{enumerate}

\item Using the following function (which was downloaded with the data set), plot all intervals. What proportion of your confidence intervals include the true population mean? Is this proportion exactly equal to the confidence level? If not, explain why.\symbolfootnote[2]{This figure should look familiar (See Section 4.2.2.)}

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{plot_ci}\hlstd{(lower_vector, upper_vector,} \hlkwd{mean}\hlstd{(population))}
\end{alltt}
\end{kframe}
\end{knitrout}


\item Pick a confidence level of your choosing, provided it is not 95\%. What is the appropriate critical value?

\item Calculate 50 confidence intervals at the confidence level you chose in the previous question. You do not need to obtain new samples, simply calculate new intervals based on the sample means and standard deviations you have already collected. Using the \hlkwd{plot\_ci} function, plot all intervals and calculate the proportion of intervals that include the true population mean. How does this percentage compare to the confidence level level selected for the intervals?

\item What concepts from the textbook are covered in this lab?  What concepts, if any, are not covered in the textbook?  Have you seen these concepts elsewhere, e.g. lecture, discussion section, previous labs, or homework problems?  Be specific in your answer.

\end{enumerate}
  
  
\end{document}
